package com.seaboxdata.controller;

import com.seaboxdata.entity.CheckRelationCondition;
import com.seaboxdata.entity.Result;
import com.seaboxdata.entity.Rkjbxx;
import com.seaboxdata.entity.Syxx;
import com.seaboxdata.service.RkjbxxService;
import com.seaboxdata.service.SecurityService;
import com.seaboxdata.service.SyxxService;
import com.seaboxdata.util.AssertMapHandler;
import com.seaboxdata.util.DecodeEncodeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author
 * @create 2021-04-12 14:40
 **/
@RestController
@EnableAutoConfiguration
@RequestMapping("/parentChild")
public class ParentChildController {

    @Autowired
    private RkjbxxService rkjbxxService;

    @Value("${isDecodeed}")
    private String isDecoded;

    @Autowired
    private SyxxService syxxService;

    @Autowired
    private SecurityService securityService;

    @Value("${syxx.key}")
    private String syxxKey;

    @Value("${jbxx.key}")
    private String jbxxKey;

    @RequestMapping("/checkRelation")
    public Map<String, Object> checkRelation(@RequestBody CheckRelationCondition condition) {
        boolean isDecodeedFlag = Boolean.valueOf(isDecoded);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("retCode", 0);
        resultMap.put("retMessage", "SUCCESS");
        Result result = new Result();
        Boolean isOnlyChild = null;
        Boolean isParentHood = null;
        resultMap.put("data", result);
        AssertMapHandler.isEmptyString(condition.getChildCardNo(), resultMap, "孩子身份证不能为空");
        AssertMapHandler.isEmptyString(condition.getChildName(), resultMap, "孩子姓名不能为空");
        AssertMapHandler.isEmptyString(condition.getFatherCardNo(), resultMap, "父亲身份证不能为空");
        AssertMapHandler.isEmptyString(condition.getFatherName(), resultMap, "父亲姓名不能为空");
        AssertMapHandler.isEmptyString(condition.getMotherCardNo(), resultMap, "母亲身份证不能为空");
        AssertMapHandler.isEmptyString(condition.getMotherName(), resultMap, "母亲姓名不能为空");
        AssertMapHandler.isEmptyString(condition.getSelectKey(), resultMap, "查询selectKey不能为空");
        AssertMapHandler.isEmptyString(condition.getAccessKey(), resultMap, "查询accessKey不能为空");
        if ((Integer) resultMap.get("retCode") != 0) {
            return resultMap;
        }

        if (!securityService.isSecurity(condition.getSelectKey(), condition.getAccessKey())) {
            resultMap.put("retCode", 1);
            resultMap.put("retMessage", "invalid selectKey or accessKey");
            return resultMap;
        }

        boolean isSingleParent = false;
        boolean isSingleFather = false;
        String motherCardNo = condition.getMotherCardNo();
        String fatherCardNo = condition.getFatherCardNo();
        if (fatherCardNo.equals(motherCardNo)) {
            isSingleParent = true;
            //判断单亲身份证号码是男还是女
            if (motherCardNo.length() < 18) {
                resultMap.put("retCode", 1);
                resultMap.put("retMessage", "不合法的母亲身份证号码,长度不为18");
                return resultMap;
            } else {
                isSingleFather = Integer.valueOf(motherCardNo.substring(16, 17)) % 2 == 0 ? false : true;
            }
        }

        //判断是否是亲子关系
        Syxx syxx = null;
        if (isDecodeedFlag) {
            syxx = syxxService.getByCardNoAndName(condition.getChildCardNo(), condition.getChildName());
        } else {
            try {
                syxx = syxxService.getByCardNoAndNameEncode(DecodeEncodeUtils.encodeSyxx(condition.getChildCardNo()),
                        DecodeEncodeUtils.encodeSyxx(condition.getChildName()));
            } catch (IOException e) {
                e.printStackTrace();
                resultMap.put("retCode", 1);
                resultMap.put("retMessage", "身份证号码或姓名加密失败");

            }
        }
        if (syxx == null) {
            resultMap.put("retCode", 1);
            resultMap.put("retMessage", "根据孩子身份证和姓名未查到生育信息");
            return resultMap;
        }

        if (isSingleParent) {
            if (isSingleFather) {
                Rkjbxx fatherRkjbxx = null;
                if (isDecodeedFlag) {
                    fatherRkjbxx = rkjbxxService.getByCardNoAndName(condition.getFatherCardNo(), condition.getFatherName());
                } else {
                    try {
                        fatherRkjbxx = rkjbxxService.getByCardNoAndNameEncode(DecodeEncodeUtils.encodeJbxx(condition.getFatherCardNo()),
                                DecodeEncodeUtils.encodeJbxx((condition.getFatherName())));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (!StringUtils.isEmpty(fatherRkjbxx.getGuid()) && fatherRkjbxx.getGuid().equals(syxx.getCurrentFatherGuid())) {
                    isParentHood = true;
                } else {
                    isParentHood = false;
                }
            } else {
                Rkjbxx motherRkjbxx = null;
                try {
                    motherRkjbxx = rkjbxxService.getByCardNoAndNameEncode(DecodeEncodeUtils.encodeJbxx(condition.getMotherCardNo()), DecodeEncodeUtils.encodeJbxx(condition.getMotherName()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (!StringUtils.isEmpty(motherRkjbxx.getGuid()) && motherRkjbxx.getGuid().equals(syxx.getCurrentMotherGuid())) {
                    isParentHood = true;
                }
            }
        } else {
            Rkjbxx fatherRkjbxx = null;
            Rkjbxx motherRkjbxx = null;
            if (isDecodeedFlag) {
                fatherRkjbxx = rkjbxxService.getByCardNoAndName(condition.getFatherCardNo(), condition.getFatherName());
                motherRkjbxx = rkjbxxService.getByCardNoAndName(condition.getMotherCardNo(), condition.getMotherName());
            } else {
                try {
                    fatherRkjbxx = rkjbxxService.getByCardNoAndNameEncode(DecodeEncodeUtils.encodeJbxx(condition.getFatherCardNo()),
                            DecodeEncodeUtils.encodeJbxx(condition.getFatherName()));
                    motherRkjbxx = rkjbxxService.getByCardNoAndNameEncode(DecodeEncodeUtils.encodeJbxx(condition.getMotherCardNo()),
                            DecodeEncodeUtils.encodeJbxx(condition.getMotherName()));
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            if (StringUtils.isEmpty(syxx.getCurrentFatherGuid()) || StringUtils.isEmpty(syxx.getCurrentMotherGuid())
                    || fatherRkjbxx == null || motherRkjbxx == null
                    || !syxx.getCurrentFatherGuid().equals(fatherRkjbxx.getGuid()) || !syxx.getCurrentMotherGuid().equals(motherRkjbxx.getGuid())) {
                isParentHood = false;
            } else {
                isParentHood = true;
            }
        }
        Rkjbxx childRkjbxx = null;
        if (isDecodeedFlag) {
            childRkjbxx = rkjbxxService.getByCardNoAndName(condition.getChildCardNo(), condition.getChildName());
        } else {
            try {
                childRkjbxx = rkjbxxService.getByCardNoAndNameEncode(DecodeEncodeUtils.encodeJbxx(condition.getChildCardNo()), DecodeEncodeUtils.encodeJbxx(condition.getChildName()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (childRkjbxx == null) {
            resultMap.put("retCode", 1);
            resultMap.put("retMessage", "根据孩子身份证和姓名未查到基本信息");
            return resultMap;
        }
        if ("1".equals(childRkjbxx.getOnlyChild())) {
            isOnlyChild = true;
        } else {
            isOnlyChild = false;
        }
        result.setOnlyChild(isOnlyChild);
        result.setParentHood(isParentHood);
        resultMap.put("data", result);
        return resultMap;
    }
}
